
layui.use(['layer','jquery','form'],function () {
    var layer = layui.layer,
        $=layui.$;
    form = layui.form;
    layer.config({
        skin: 'layui-layer-lan'
    });

    //页面初始化生成验证码
    window.onload = load('#loginCode');
    // 验证码切换
    $('#loginCode').click(function () {
        createCode('#loginCode');
    });
    // 登陆事件
    $('#loginBtn').click(function () {
        login();
    });
    // 注册事件
    $('#loginRegister').click(function () {
        clearregister();
        register();
    });
    //ajax验证是否已经存在用户名，验证重复
    $(document).on('blur','#registerUsername',function () {
        var registerUsername=$('#registerUsername').val();
        var params={};
        if(registerUsername!=null && registerUsername!=""){
            params.registerUsername=registerUsername;

            $.ajax({
                type:'post',
                url:'/user/getadmincount',
                dataType:'text',
                data:JSON.stringify(params),
                contentType:'application/json',
                success:function (data) {
                    if (data=='ff'){
                        $('#registerUsername').css({border:"1px solid red"});
                        $('#info').html("此用户名已被注册");
                    }
                }

            });
        }
    });
    //清空提示信息
    $(document).on('focus','#registerUsername',function () {
        $('#registerUsername').css({border:""});
        $('#info').html(" ");
    });

    function load(codeID){
        clearload();
        createCode(codeID);
    }

    //清除原来的页面数据
    function clearload() {
        $('#loginPassword').val("");
        $('#loginCard').val("");

    }
    // 生成验证码
    function createCode(codeID) {
        var code = "";
        // 验证码长度
        var codeLength = 4;
        // 验证码dom元素
        var checkCode = $(codeID);
        // 验证码随机数
        var random = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
            'S','T','U','V','W','X','Y','Z'];
        for (var i = 0;i < codeLength; i++){
            // 随机数索引
            var index = Math.floor(Math.random()*36);
            code += random[index];
        }
        // 将生成的随机验证码赋值
        checkCode.val(code);
    }
    // 校验验证码、用户名、密码
    function validateCode(inputID,codeID) {
        var inputCode = $(inputID).val().toUpperCase();
        var cardCode = $(codeID).val();
        var loginUsername = $('#loginUsername').val();
        var loginPassword = $('#loginPassword').val();
        if ($.trim(loginUsername) == '' || $.trim(loginUsername).length<=0){
            layer.alert("用户名不能为空",{icon:5});
            return false;
        }
        if ($.trim(loginPassword) == '' || $.trim(loginPassword).length<=0){
            layer.alert("密码不能为空",{icon:5});
            return false;
        }
        if (inputCode.length<=0){
            layer.alert("验证码不能为空",{icon:5});
            return false;
        }
        if (inputCode != cardCode){
            layer.alert("请输入正确验证码",{icon:5});
            return false;
        }
        return true;
    }
    // 登录流程
    function login() {
        if (!validateCode('#loginCard','#loginCode')){
            //阻断提示
        }else {

            var loginLoadIndex = layer.load(2);
            $('#loginBtn').val("正在登录...");
            $.ajax({
                type:'post',
                url:'/user/login',
                // dataType:'json',
                data:$('.layui-form').serialize(),
                success:function (data) {
                    layer.close(loginLoadIndex);

                    if (data=='success'){
                        layer.msg('登陆成功',{time:2000,icon:1,end:function(){
                                window.location.href = './views/index.jsp';
                            }})

                    }else if (data=='failure'){
                        layer.close(loginLoadIndex);
                        $('#loginBtn').val("登录");
                        layer.msg('登陆失败',{time:2000,icon:5})
                    }
                },
                error:function () {
                    layer.close(loginLoadIndex);
                    $('#loginBtn').val("登录");
                }
            });
        }

    }


    function clearregister(){
        $('#registerUsername').val("");
        $('#registerPassword').val("");
        $('#registerWellPassword').val("");
    }
    // 注册流程
    function register() {
        var index=layer.open({
            type:'1',
            content:$('.registerPage'),
            title:'注册',
            area:['430px','400px'],
            btn:['注册','重置','取消'],
            closeBtn:'1',
            btn1:function (index,layero) {
                //注册回调
                // layer.close(index);
                var registerUsername = $('#registerUsername').val();
                var registerPassword = $('#registerPassword').val();
                var registerWellPassword = $('#registerWellPassword').val();
                var params = {};
                if(!validateRegister()){
                    //先进行验证
                }else{
                    params.registerUsername = registerUsername;
                    params.registerPassword = registerPassword;
                    params.registerWellPassword = registerWellPassword;
                    var registerLoadIndex = layer.load(2);
                    $.ajax({
                        type:'post',
                        url:'/user/register',
                        async:false,
                        dataType:'text',
                        data:JSON.stringify(params),
                        contentType:'application/json',
                        success:function (data) {
                            if(data=='success'){
                                layer.close(registerLoadIndex);
                                layer.msg("注册成功",{title:"提示",timeout:2000,icon:1},function () {
                                    layer.close(index);

                                });


                            }else if(data='failure'){
                                layer.close(registerLoadIndex);
                                layer.msg("注册失败",{title:"提示",timeout:2000,icon:5});
                            }


                        },
                        error:function () {
                            layer.close(registerLoadIndex);
                            layer.alert("请求超时！")
                        }
                    });
                }

            },
            btn2:function (index,layero) {
                //重置回调
                var registerUsername = $('#registerUsername').val("");
                var registerPassword = $('#registerPassword').val("");
                var registerWellPassword = $('#registerWellPassword').val("");
                // 防止注册页面关闭
                return false;
            },
            btn3:function (index,layero) {
                //取消回调
            }
        })
    }

    function validateRegister() {
        var registerUsername = $('#registerUsername').val();
        var registerPassword = $('#registerPassword').val();
        var registerWellPassword = $('#registerWellPassword').val();
        if ($.trim(registerUsername) == '' || $.trim(registerUsername).length<=0){
            layer.alert("用户名不能为空",{icon:5});
            return false;
        }
        if ($.trim(registerPassword) == '' || $.trim(registerPassword).length<=0){
            layer.alert("密码不能为空",{icon:5});
            return false;
        }
        if ($.trim(registerWellPassword) == '' || $.trim(registerWellPassword).length<=0){
            layer.alert("重复密码不能为空",{icon:5});
            return false;
        }
        if (registerPassword!=registerWellPassword){
            layer.alert("两次输入的密码不相符，请重新输入",{icon:5});
            return false;
        }

        return true;
    }

})

